from django.http import HttpResponse
from django.shortcuts import render
from TestModel.models import Test


def reg_form(request):
    return render(request, "user_reg.html")


def reg_post(request):
    if request.method == "POST":
        name = request.POST["user_name"]
        pwd = request.POST["pwd"]
        repeat_pwd = request.POST["repeat_pwd"]
        if pwd == repeat_pwd:
            user = Test(name=name, pwd=pwd)
            user.save()
            return render(request, "reg_suc.html", {"name":name})
        else:
            return render(request, "user_reg.html", {"d":"请重新输入密码"})


def login(request):
    return render(request, "user_login.html", {"d":""})
def login_post(request):
    if request.method == "POST":
        user = Test.objects.get(name=request.POST["user_name"])
        print(user)
        if user is None:
            print("名错误")
            return render(request, "user_login.html", {"d": "用户名错误"})
        else:
            if user.pwd == request.POST["pwd"]:
                print("成功")
                return render(request, "user.html")
            else:
                print("密错误")
                return render(request, "user_login.html", {"d": "密码错误"})
